﻿// <copyright file="Wall.xaml.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace Channel9Kinect
{
    #region

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Navigation;

    using Channel9Kinect.Navigation;

    #endregion

    /// <summary>
    ///   Interaction logic for Wall.xaml
    /// </summary>
    public partial class Wall : Page, IPageParameter<Tuple<Uri, Uri>>
    {
        #region Constants and Fields

        private readonly WallViewModel wallvm;

        private static Tuple<Uri, Uri> lasteIntoParameter;

        private NavigationService navigationService;

        #endregion

        #region Constructors and Destructors

        public Wall()
        {
            this.InitializeComponent();
            this.wallvm = (WallViewModel)this.Resources["wallvm"];
            this.Loaded += this.Wall_Loaded;
            this.Unloaded += this.Wall_Unloaded;
        }

        #endregion

        #region Public Methods

        public void LoadParameter(Tuple<Uri, Uri> paramerter)
        {
            this.wallvm.DefaultImage = paramerter.Item2;
            this.wallvm.FeedUri = paramerter.Item1;
            lasteIntoParameter = paramerter;
        }

        #endregion

        #region Methods

        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.NavigationService.GoBack();
        }

        private void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.Back)
            {
                lasteIntoParameter = null;
            }
        }

        private void Wall_Loaded(object sender, RoutedEventArgs e)
        {
            this.navigationService = this.NavigationService;
            this.navigationService.Navigating += this.NavigationService_Navigating;
            //When navigating back to the page. Reload parameters
            if (lasteIntoParameter != null)
            {
                this.LoadParameter(lasteIntoParameter);
            }
        }

        private void Wall_Unloaded(object sender, RoutedEventArgs e)
        {
            this.navigationService.Navigating -= this.NavigationService_Navigating;
        }

        #endregion
    }
}